var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var common = {};

// 
common.extend=function(o,n){
  for(var p in n)if(n.hasOwnProperty(p) && !o.hasOwnProperty(p))o[p]=n[p];
  return o;
}
// 别名
common.resolve = {
	root:[path.join(__dirname, 'app/src/vendor')],
    extensions: ['', '.js', '.jsx', '.css','.scss','sass'],
    alias:{
    	src:      path.join(__dirname, './app/src'),
			vendor:   path.join(__dirname, './app/src/vendor'),
			modules: 	path.join(__dirname, './app/src/modules'),
      common:   path.join(__dirname, './app/src/commons'),

      backbone: path.join(__dirname, './app/src/modules/backbone'),
			blog: 		path.join(__dirname, './app/src/modules/blog'),
			case: 		path.join(__dirname, './app/src/modules/case'),

			
		
    }
}

// 公共库
common.vendor = ['jquery']

// 输出
common.output = {
  path: path.join(__dirname,'app/build'),
  filename: '[name].js'
}

// loaders
common.loaders = [{
    test: /\.css$/,
    loader: ExtractTextPlugin.extract('style-loader','css-loader?modules&importLoaders=2&localIdentName=[local]!autoprefixer-loader')
  },{
    test: /\.(sass|scss)$/,
    loader: ExtractTextPlugin.extract('style-loader','css-loader?modules&importLoaders=2&localIdentName=[local]___[hash:base64:5]!autoprefixer-loader!sass-loader'),
    exclude: [path.join(__dirname,'app/src/commons/style')]
  },{
    test: /\.(sass|scss)$/,
    loader: ExtractTextPlugin.extract('style-loader','css-loader!autoprefixer-loader!sass-loader'),
    include: [path.join(__dirname,'app/src/commons/style')]
  },{
    test: /\.less$/,
    loader: ExtractTextPlugin.extract('style-loader','css-loader!autoprefixer-loader!less-loader')
  },{
    test: /\.(png|jpg|jpeg)$/,
    loader: 'url-loader?limit=8192&name=img/[hash].[ext]'
  },{
    test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
    loader: "url-loader?limit=10000&name=resource/[hash].[ext]&minetype=application/font-woff"
  },{
    test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
    loader: "file-loader?name=resource/[hash].[ext]"
  }
]

common.plugins = [
	new webpack.NoErrorsPlugin(),
  // 打包公共库
  new webpack.optimize.CommonsChunkPlugin({
    name: "vendor",
    filename: "vendor.js"
  }),
  // bower 文件
  new webpack.ResolverPlugin(
      new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
  ),
  // 全局变量
  new webpack.ProvidePlugin({
      $: "jquery",
      jQuery:'jquery',
      React:'react',
      cn:'classname',
      mui:'material-ui'
  }),
  // css 文件单独打包
  new ExtractTextPlugin("style.css", {
      allChunks: true
  }),

]

module.exports = common;
